package com.epfresh.supplier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.epfresh.api.dialog.TimePickerDialog;
import com.epfresh.api.entity.RequestEntityMapSupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.entity.StatusEntity;
import com.epfresh.api.global.AppActivity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.supplier.R;
import com.google.gson.Gson;

import java.util.Calendar;

/**
 * Created by wzh on 16-7-25.
 */
public class ProductTimeActivity extends AppActivity {


    private TextView tvTime;
    private TextView tvTitle;

    //type == 1 库存,type==2起订量
    private String time;
    private String productId;
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_product_time);

        if (savedInstanceState != null) {
            getIntent().putExtra("productId", savedInstanceState.getString("productId"));
            getIntent().putExtra("time", savedInstanceState.getString("time"));
            getIntent().putExtra("type", savedInstanceState.getInt("type",-1));

        }

        type = getIntent().getIntExtra("type",-1);
        time = getIntent().getStringExtra("time");
        ivBack.setVisibility(View.VISIBLE);
        tvBack.setVisibility(View.VISIBLE);

        tvTime = (TextView) findViewById(R.id.tv_package);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        if(type == 1) {
            toolbarTitle.setText("预计到货时间");
        }else {
            toolbarTitle.setText("进货日期");
            tvTitle.setText("进货日期");
        }
        rightbtn.setText("保存");
        rightbtn.setVisibility(View.VISIBLE);



        tvTime.setText(time);

        findViewById(R.id.rl_time).setOnClickListener(this);


    }

    @Override
    public void onRightClick() {
        save();
        finish();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {

            case R.id.rightbtn:

                break;
            case R.id.rl_time:
                openMarketTime(tvTime.getText().toString());
                break;

        }
    }

    private void save() {

        if ("".equals(tvTime.getText().toString())) {
            return;
        }
        Intent intent = new Intent();
        intent.putExtra("time", time);
//        intent.putExtra("brand",brand);
//        intent.putExtra("time",time);

        setResult(RESULT_OK, intent);

        if (productId != null && !"".equals(productId)) {
            requestProductModify(time);
        } else
            finish();
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("time", time);
        outState.putString("productId", productId);
        outState.putInt("type", type);
        super.onSaveInstanceState(outState);
    }

    private void openMarketTime(String time) {

        TimePickerDialog timeDialog = new TimePickerDialog(this);

        if(type == 1) {
            timeDialog.setTitle("预计到货时间");
        }else {
            timeDialog.setTitle("进货日期");
        }
        timeDialog.setOnPositiveClickListener(new TimePickerDialog.OnPositiveClickListener() {
            @Override
            public void onPositiveClickListener(int index, String data) {
                tvTime.setText(data);
                ProductTimeActivity.this.time = data;

            }
        });

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        if(type == 1){//预计到货时间

            calendar.add(Calendar.DAY_OF_MONTH,3);
        }else {
            calendar.add(Calendar.MONTH, -6);
        }
        int sy = calendar.get(Calendar.YEAR);
        int sm = calendar.get(Calendar.MONTH) + 1;
        int sd = calendar.get(Calendar.DAY_OF_MONTH);

        if(type == 1){//预计到货时间
            calendar.add(Calendar.YEAR, 1);
        }else {
            calendar.setTimeInMillis(System.currentTimeMillis());
        }
        int ey = calendar.get(Calendar.YEAR);
        int em = calendar.get(Calendar.MONTH) + 1;
        int ed = calendar.get(Calendar.DAY_OF_MONTH);


        timeDialog.setStartYear(sy);
        timeDialog.setStartMonth(sm);
        timeDialog.setStartDay(sd);

        timeDialog.setEndYear(ey);
        timeDialog.setEndMonth(em);
        timeDialog.setEndDay(ed);

        timeDialog.setCurrentDate(time);

        timeDialog.show();
    }

    private void requestProductModify(String deliveryTime) {

        RequestEntityMapSupply RequestEntityMapSupply = new RequestEntityMapSupply();
        RequestEntityMapSupply.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_product_modify);


        RequestEntityMapSupply.putParameter("deliveryTime", deliveryTime);
        RequestEntityMapSupply.putParameter("id", productId);
        request(RequestEntityMapSupply, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_product_modify, onProductModifyRequestListener);

    }

    private OnRequestListener<StatusEntity> onProductModifyRequestListener = new OnRequestListener<StatusEntity>() {
        @Override
        public StatusEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, StatusEntity.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<StatusEntity> response, Object tag) {

            hideProgressDialog();
            StatusEntity responseElement = response.getResponseElement();
            if (responseElement.isSuccess()) {
                finish();
            } else {
                //没有数据
                toast("修改失败");

            }

        }
    };

}
